package cn.ava.system.controller;

import cn.ava.basic.annotation.PreAuthorize;
import cn.ava.org.domain.Department;
import cn.ava.system.service.IMenuService;
import cn.ava.system.domain.Menu;
import cn.ava.system.query.MenuQuery;
import cn.ava.basic.utils.AjaxResult;
import cn.ava.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    public IMenuService menuService;

    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace(); 
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            menuService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Menu getById(@PathVariable("id")Long id)
    {
        return menuService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Menu> list(){
        return menuService.loadAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Menu> json(@RequestBody MenuQuery query)
    {
        return menuService.queryData(query);
    }

    @PatchMapping
    public AjaxResult batchDelete(@RequestBody Long[] id) {
        try {
            menuService.batchDelete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    @GetMapping("/tree")
    @PreAuthorize(name = "获取部门树数据",sn = "department:DeptTree")
    public List<Menu> getMenuTree() {
        return menuService.getMenuTree();
    }



}
